package m.jclecai.com;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.view.Display;
import android.view.WindowManager;

import com.mob.MobApplication;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import m.jclecai.com.bean.DebugBean;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.utils.ABPreferenceUtils;
import m.jclecai.com.utils.ChannelUtil;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.SystemUtil;

/**
 * Created by hp on 2017/4/6.
 */

public class MyApplication extends MobApplication {
    public static String HTTP_URL = BuildConfig.HOST_NAME;
    //开关
    public static String HTTP_URL_SWITCH = "http://m.fleaphp.com/";
    //首页菜单
    public static String HTTP_URL_MENU = "http://static.api.sports.letv.com/";

    // 名家解说
    public static String HTTP_URL_RECOMMEND = "http://j5.yuecp.net/";

//    public static String HTTP_URL = "https://j5.yuecp.net/";

    //自行切换服务器类型
    // static String LOTTERY_TYPE = "PHP";
    static String LOTTERY_TYPE = "JAVA";
    public static boolean ISPHP = "PHP".equals(LOTTERY_TYPE);
    public static String SERVERPARAM = "1";      //1 表示php，2表示java

    private static Typeface mPokerTypeFace;

    //选择的方案

    public void setCount(String count) {
        this.count = count;
    }

    public void setCountContinuous(String countContinuous) {
        this.countContinuous = countContinuous;
    }

    public void setProfit(String profit) {
        this.profit = profit;
    }


    public void setProfitCount(String profitCount) {
        this.profitCount = profitCount;
    }

    public void setProfitBefore(String profitBefore) {
        this.profitBefore = profitBefore;
    }

    public void setProfitAfter(String profitAfter) {
        this.profitAfter = profitAfter;
    }

    public void setPercentCount(String percentCount) {
        this.percentCount = percentCount;
    }

    public void setPercentBefore(String percentBefore) {
        this.percentBefore = percentBefore;
    }

    public void setPercentAfter(String percentAfter) {
        this.percentAfter = percentAfter;
    }

    public String getCount() {

        return count;
    }

    public String getCountContinuous() {
        return countContinuous;
    }

    public String getProfit() {
        return profit;
    }


    public String getProfitCount() {
        return profitCount;
    }

    public String getProfitBefore() {
        return profitBefore;
    }

    public String getProfitAfter() {
        return profitAfter;
    }

    public String getPercentCount() {
        return percentCount;
    }

    public String getPercentBefore() {
        return percentBefore;
    }

    public String getPercentAfter() {
        return percentAfter;
    }

    private int item;
    private String count;
    private String countContinuous;
    private String profit;
    private String profitCount;
    private String profitBefore;
    private String profitAfter;
    private String percentCount;
    private String percentBefore;
    private String percentAfter;
    private String profitPercent;
    public boolean isNull = true;
    /**
     *  开关状态   0 开  1 关
     */
    public static int status = 0;

    public void setProfitPercent(String profitPercent) {
        this.profitPercent = profitPercent;
    }

    public String getProfitPercent() {

        return profitPercent;
    }

    public int getItem() {
        return item;
    }

    public void setItem(int item) {

        this.item = item;
    }

    private static MyApplication instance;

    public static List<Activity> areaListStack = new ArrayList<>();
    public static Stack<Activity> activityStack = new Stack<>();

    //-------------------------------------------------------------

    public int displayHeight = 0;
    public int displayWith = 0;
    private UserBean userBean;

    @Override
    public void onCreate() {
        super.onCreate();
        //网络请求的封装
        initWindow();
        userBean = new UserBean();
        //如果您原先就继承了其他application，或者不方便修改，
        // 则可以不继承MobApplication，只要在使用ShareSDK之前，调用以下代码：
        // 通过代码注册你的AppKey和AppSecret
//        MobSDK.init(this);
        DebugBean debugBean = PreferenceUtil.getDebugBean(this);
        if (debugBean != null
                && !TextUtils.isEmpty(debugBean.getHttpUrl())
                && (debugBean.getHttpUrl().startsWith("http")
                || debugBean.getHttpUrl().startsWith("https"))) {
            HTTP_URL = debugBean.getHttpUrl();
        }

        if (debugBean != null && !TextUtils.isEmpty(debugBean.getParam())
                && !debugBean.getParam().startsWith("--")) {
            LOTTERY_TYPE = debugBean.getParam();
        }
        instanceParamType();

        instance = this;

        //设置样式：
        mPokerTypeFace = Typeface.createFromAsset(this.getAssets(), "typewriter.ttf");
        initBugly();

        ABPreferenceUtils.getInstance(getApplicationContext());
    }

    public static Typeface getPokerTypeFace() {
        return mPokerTypeFace;
    }




    private void initBugly() {
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = SystemUtil.getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setAppChannel(ChannelUtil.getChannel());
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(getApplicationContext(), strategy);
    }

    //初始化 对应服务的参数
    public void instanceParamType() {
        switch (LOTTERY_TYPE) {
            case "JAVA":
                ISPHP = false;
                SERVERPARAM = "2";
                break;
            case "PHP":
                SERVERPARAM = "1";
                ISPHP = true;
                break;
        }
    }

    private void initWindow() {
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        displayWith = display.getWidth();
        displayHeight = display.getHeight();
    }

    public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    /**
     * 创建一个单例的Application
     */
    public static MyApplication getInstance() {
        if (null == instance) {
            synchronized (MyApplication.class) {//保证异步处理安全操作
                if (null == instance) {
                    instance = new MyApplication();
                }
            }
        }
        return instance;
    }

    /**
     * 获取application的实例
     */
    public static Context getAppContext() {
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public static void addActivity(Activity activity) {
        activityStack.push(activity);
    }

    /**
     * 结束所有Activity
     */
    public static void finishAllAreaActivity() {
        for (Activity activity : areaListStack) {
            if (activity != null) {
                activity.finish();
            }
        }
        areaListStack.clear();
    }

    /**
     * 结束所有Activity
     */
    public static void over() {
        for (Activity activity : activityStack) {
            if (activity != null) {
                activity.finish();
            }
        }
        activityStack.clear();
    }

    public List<Activity> getAreaListStack() {
        return areaListStack;
    }

    public void setAreaListStack(List<Activity> areaListStack) {
        this.areaListStack = areaListStack;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
